﻿@page


<div class="row h-100">
    <div class="col-3" style="height:100%;padding-right:30px;overflow-y:scroll" id="mempanel">
        地址

        <div  class="row">
        @for (int i = 16483; i >= 100; i -= 4) //16k内存
        {
            <div class="row">
                <label class="col-2 col-form-label" style="padding-top:0px">0x@((i-3).ToString("X4"))</label>
                <div class="col-1"><input  style="width:40px;font-size:12px" type="text" id="m@(i-3)" readonly /></div>
                <div class="col-1"><input  style="width:40px;font-size:12px" type="text" id="m@(i-2)" readonly /></div>
                <div class="col-1"><input  style="width:40px;font-size:12px" type="text" id="m@(i-1)" readonly /></div>
                <div class="col-1"><input  style="width:40px;font-size:12px" type="text" id="m@(i)" readonly /></div>
                <label class="col-6 col-form-label" id="c@((i+1)/4-1)" style="padding-top:0px"></label>
           </div>
        }
        </div>


        <div style="background-color:yellow" class="row">
        @for (int i = 99; i >= 0; i -= 4)
        {
            <div class="row">
                <label class="col-2 col-form-label" style="padding-top:0px">0x@((i-3).ToString("X4"))</label>
                <div class="col-1"><input style="width:40px;font-size:12px"  type="text" id="m@(i-3)" readonly /></div>
                <div class="col-1"><input style="width:40px;font-size:12px"  type="text" id="m@(i-2)" readonly /></div>
                <div class="col-1"><input style="width:40px;font-size:12px"  type="text" id="m@(i-1)" readonly /></div>
                <div class="col-1"><input style="width:40px;font-size:12px" type="text" id="m@(i)" readonly /></div>
                 <label class="col-6 col-form-label" id="c@((i+1)/4-1)" style="padding-top:0px"></label>
          </div>
        }
        </div>
    </div>
    <div class="col-6 d-flex flex-column ">
        <div class="row flex-fill flex-column">
            <div class="row" style="height:30px;">显示器</div>

            <div class="row flex-fill">
                <div style="background-color:green;border:solid">
                    <ul id="messagesList"></ul>

                </div>
            </div>

        </div>
        <div class="row" style="height:300px;margin-top:20px;">
            <div class="row">
                <textarea class="form-control" id="messageInput" rows="10" style="height:220px;"></textarea>
            </div>
            <div class="row" style="height:30px;">
                <div class="col-12 text-end">
                    <button class="btn btn-primary mb-3" data-bs-toggle="modal" data-bs-target="#debugwindow">断点</button>
                  <button class="btn btn-primary mb-3" data-bs-toggle="modal" data-bs-target="#writewindow">写数据</button>
                    <button class="btn btn-primary mb-3" data-bs-toggle="modal" data-bs-target="#bookwindow">查手册</button>
                    <button class="btn btn-primary mb-3" id="run">运行</button>
                    <button class="btn btn-primary mb-3" id="pause">暂停</button>
                    <button class="btn btn-primary mb-3" id="runstep">单步</button>
                    <button class="btn btn-primary mb-3" id="reset">重启</button>
                </div>
            </div>
        </div>
    </div>
    <div class="col-3" style="height:100%;padding-right:30px;overflow-y:scroll">
        寄存器
        <div class="row">
            <label class="col-2 col-form-label">X0(zero)</label>
            <div class="col-4"><input class="form-control" type="text" id="x0" readonly /></div>
            <label class="col-2 col-form-label">X1(ra)</label>
            <div class="col-4"><input class="form-control" type="text" id="x1" readonly /></div>
        </div>
        <div class="row">
            <label class="col-2 col-form-label">X2(sp)</label>
            <div class="col-4"><input class="form-control" type="text" id="x2" readonly /></div>
            <label class="col-2 col-form-label">X3(gp)</label>
            <div class="col-4"><input class="form-control" type="text" id="x3" readonly /></div>
        </div>
        <div class="row">
            <label class="col-2 col-form-label">X4(tp)</label>
            <div class="col-4"><input class="form-control" type="text" id="x4" readonly /></div>
            <label class="col-2 col-form-label">X5(t0)</label>
            <div class="col-4"><input class="form-control" type="text" id="x5" readonly /></div>
        </div>
        <div class="row">
            <label class="col-2 col-form-label">X6(t1)</label>
            <div class="col-4"><input class="form-control" type="text" id="x6" readonly /></div>
            <label class="col-2 col-form-label">X7(t2)</label>
            <div class="col-4"><input class="form-control" type="text" id="x7" readonly /></div>
        </div>
        <div class="row">
            <label class="col-2 col-form-label">X8(s0)</label>
            <div class="col-4"><input class="form-control" type="text" id="x8" readonly /></div>
            <label class="col-2 col-form-label">X9(s1)</label>
            <div class="col-4"><input class="form-control" type="text" id="x9" readonly /></div>
        </div>
        <div class="row">
            <label class="col-2 col-form-label">X10(a0)</label>
            <div class="col-4"><input class="form-control" type="text" id="x10" readonly /></div>
            <label class="col-2 col-form-label">X11(a1)</label>
            <div class="col-4"><input class="form-control" type="text" id="x11" readonly /></div>
        </div>
        <div class="row">
            <label class="col-2 col-form-label">X12(a2)</label>
            <div class="col-4"><input class="form-control" type="text" id="x12" readonly /></div>
            <label class="col-2 col-form-label">X13(a3)</label>
            <div class="col-4"><input class="form-control" type="text" id="x13" readonly /></div>
        </div>
        <div class="row">
            <label class="col-2 col-form-label">X14(a4)</label>
            <div class="col-4"><input class="form-control" type="text" id="x14" readonly /></div>
            <label class="col-2 col-form-label">X15(a5)</label>
            <div class="col-4"><input class="form-control" type="text" id="x15" readonly /></div>
        </div>
        <div class="row">
            <label class="col-2 col-form-label">X16(a6)</label>
            <div class="col-4"><input class="form-control" type="text" id="x16" readonly /></div>
            <label class="col-2 col-form-label">X17(a7)</label>
            <div class="col-4"><input class="form-control" type="text" id="x17" readonly /></div>
        </div>
        <div class="row">
            <label class="col-2 col-form-label">X18(s2)</label>
            <div class="col-4"><input class="form-control" type="text" id="x18" readonly /></div>
            <label class="col-2 col-form-label">X19(s3)</label>
            <div class="col-4"><input class="form-control" type="text" id="x19" readonly /></div>
        </div>
        <div class="row">
            <label class="col-2 col-form-label">X20(s4)</label>
            <div class="col-4"><input class="form-control" type="text" id="x20" readonly /></div>
            <label class="col-2 col-form-label">X21(s5)</label>
            <div class="col-4"><input class="form-control" type="text" id="x21" readonly /></div>
        </div>
        <div class="row">
            <label class="col-2 col-form-label">X22(s6)</label>
            <div class="col-4"><input class="form-control" type="text" id="x22" readonly /></div>
            <label class="col-2 col-form-label">X23(s7)</label>
            <div class="col-4"><input class="form-control" type="text" id="x23" readonly /></div>
        </div>
        <div class="row">
            <label class="col-2 col-form-label">X24(s8)</label>
            <div class="col-4"><input class="form-control" type="text" id="x24" readonly /></div>
            <label class="col-2 col-form-label">X25(s9)</label>
            <div class="col-4"><input class="form-control" type="text" id="x25" readonly /></div>
        </div>
        <div class="row">
            <label class="col-2 col-form-label">X26(s10)</label>
            <div class="col-4"><input class="form-control" type="text" id="x26" readonly /></div>
            <label class="col-2 col-form-label">X27(s11)</label>
            <div class="col-4"><input class="form-control" type="text" id="x27" readonly /></div>
        </div>
        <div class="row">
            <label class="col-2 col-form-label">X28(t3)</label>
            <div class="col-4"><input class="form-control" type="text" id="x28" readonly /></div>
            <label class="col-2 col-form-label">X29(t4)</label>
            <div class="col-4"><input class="form-control" type="text" id="x29" readonly /></div>
        </div>
        <div class="row">
            <label class="col-2 col-form-label">X30(t5)</label>
            <div class="col-4"><input class="form-control" type="text" id="x30" readonly /></div>
            <label class="col-2 col-form-label">X31(t6)</label>
            <div class="col-4"><input class="form-control" type="text" id="x31" readonly /></div>
        </div>
        <div class="row">
            <label class="col-2 col-form-label">PC</label>
            <div class="col-4"><input class="form-control" type="text" id="x100" readonly /></div>
        </div>

    </div>
</div>
<div class="modal fade" id="bookwindow" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="staticBackdropLabel">命令手册</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="mb-3">
                    <label for="exampleFormControlInput1" class="form-label">选择命令</label>
                    <select class="form-select" aria-label="请选择" id="bookcmd" onchange="handleChange(this)">
                        <option value="" selected>请选择</option>
                        <optgroup label="RType">
                            <option value="ADD">ADD(加)</option>
                            <option value="SUB">SUB(减)</option>
                            <option value="MUL">MUL(乘)</option>
                            <option value="AND">AND(与)</option>
                            <option value="OR">OR(或)</option>
                            <option value="XOR">XOR(异或)</option>
                            <option value="SLL">SLL(逻辑左移)</option>
                            <option value="SRL">SRL(逻辑右移)</option>
                            <option value="SRA">SRA(算数右移)</option>
                            <option value="SLT">SLT(比较)</option>
                            <option value="SLTU">SLTU(无符号比较)</option>
                        </optgroup>
                        <optgroup label="IType">
                            <option value="ADDI">ADDI(加)</option>
                            <option value="ANDI">ANDI(与)</option>
                            <option value="ORI">ORI(或)</option>
                            <option value="XORI">XORI(异或)</option>
                            <option value="SLLI">SLLI(逻辑左移)</option>
                            <option value="SRLI">SRLI(逻辑右移)</option>
                            <option value="SRAI">SRAI(算数右移)</option>
                            <option value="SLTI">SLTI(比较)</option>
                            <option value="SLTIU">SLTIU(无符号比较)</option>
                            <option value="LB">LB(加载8位数)</option>
                            <option value="LBU">LBU(加载无符号8位数)</option>
                            <option value="LH">LH(加载16位数)</option>
                            <option value="LHU">LHU(加载无符号16位数)</option>
                            <option value="LW">LW(加载32位数)</option>
                            <option value="JALR">JALR(绝对跳转)</option>
                        </optgroup>
                        <optgroup label="BType">
                            <option value="BEQ">BEQ(相等时跳转)</option>
                            <option value="BNE">BNE(不等时跳转)</option>
                            <option value="BLT">BLT(小于时跳转)</option>
                            <option value="BLTU">BLTU(无符号小于时跳转)</option>
                            <option value="BGE">BGE(大于等于时跳转)</option>
                            <option value="BGEU">BGEU(无符号大于等于时跳转)</option>
                        </optgroup>
                        <optgroup label="SType">
                            <option value="SB">SB(存储8位数)</option>
                            <option value="SH">SH(存储16位数)</option>
                            <option value="SW">SW(存储32位数)</option>
                        </optgroup>
                        <optgroup label="UType">
                            <option value="AUIPC">AUIPC(取高位加PC)</option>
                            <option value="LUI">LUI(取高位)</option>
                        </optgroup>
                        <optgroup label="JType">
                            <option value="JAL">JAL(相对跳转)</option>
                        </optgroup>
                    </select>
                </div>
                <div class="mb-3" id="bookdes">
                </div>
                <div class="mb-3" id="rs1row" style="display:none">
                    <label for="exampleFormControlTextarea1" class="form-label">源寄存器1</label>
                    <input class="form-control" id="bookrs1" placeholder="输入rs1的编号">
                </div>
                <div class="mb-3" id="rs2row" style="display:none">
                    <label for="exampleFormControlTextarea1" class="form-label">源寄存器2</label>
                    <input class="form-control" id="bookrs2" placeholder="输入rs2的编号">
                </div>
                <div class="mb-3" id="rdrow" style="display:none">
                    <label for="exampleFormControlTextarea1" class="form-label">目标寄存器</label>
                    <input class="form-control" id="bookrd" placeholder="输入rd的编号">
                </div>
                <div class="mb-3" id="immrow" style="display:none">
                    <label for="exampleFormControlTextarea1" class="form-label">立即数</label>
                    <input class="form-control" id="bookimm" placeholder="输入imm">
                </div>
                <script>
                    function handleChange(a,b) {
                        var obj = document.getElementById('bookcmd');
                        var groupname = obj.selectedOptions[0].parentElement.label;
                        var index = obj.selectedIndex;
                        var value = obj.options[index].value;
                        var rs1row = document.getElementById('rs1row');
                        var rs2row = document.getElementById('rs2row');
                        var rdrow = document.getElementById('rdrow');
                         var immrow = document.getElementById('immrow');
                         var bookrs1 = document.getElementById('bookrs1');
                         var bookrs2 = document.getElementById('bookrs2');
                         var bookrd = document.getElementById('bookrd');
                         var bookimm = document.getElementById('bookimm');
                         bookrs1.value = "";
                         bookrs2.value = "";
                         bookrd.value = "";
                         bookimm.value = "";
                       var des = "";
                       switch(groupname){
                           case "IType":
                           rs1row.style.display = 'block';
                           rs2row.style.display = 'none';
                           rdrow.style.display = 'block';
                           immrow.style.display = 'block';
                           break;
                           case "RType":
                            rs1row.style.display = 'block';
                           rs2row.style.display = 'block';
                           rdrow.style.display = 'block';
                           immrow.style.display = 'none';
                          break;
                          case "JType":
                           case "UType":
                             rs1row.style.display = 'none';
                           rs2row.style.display = 'none';
                           rdrow.style.display = 'block';
                           immrow.style.display = 'block';
                         break;
                          case "SType":
                           case "BType":
                           rs1row.style.display = 'block';
                           rs2row.style.display = 'block';
                           rdrow.style.display = 'none';
                           immrow.style.display = 'block';
                          break;
                       }
                        switch(value)
                        {
                            case "ADD":
                                des = "将rs1和rs2中的值相加，并将结果写入rd";
                             break;
                           case "AND":
                                des = "将rs1和rs2中的值按位进行与操作，并将结果写入rd";
                             break;
                           case "OR":
                                des = "将rs1和rs2中的值按位进行或操作，并将结果写入rd";
                            break;
                            case "SLL":
                                des = "SLL(Shift Left Logical) 将rs1的值逻辑左移rs2中的值，并将结果写入rd";
                            break;
                            case "SLT":
                                des = "SLT(Set Less Then)如果rs1的值小于rs2中的值，rd的值设为1，否则rd的值设为0";
                            break;
                            case "SLTU":
                                des = "SLTU(Set Less Then Unsigned)如果rs1的值小于rs2中的值（无符号对比），rd的值设为1，否则rd的值设为0<br/> 特殊处理，如果rs1是x0，则当rs2中的值等于0时，rd的值为1，否则rd的值为0";
                            break;
                            case "SRA":
                                des = "SRA(Shift Right Arithmetic) 将rs1的值算数右移rs2中的值，并将结果写入rd，算数右移意味着如果是正数，左边补零，如果是负数，左边补1";
                            break;
                            case "SRL":
                                des = "SRL(Shift Right Logical) 将rs1的值逻辑右移rs2中的值，并将结果写入rd，逻辑右移意味着不管正数还是负数，左边都补0";
                            break;
                            case "SUB":
                                des = "将rs1中的值减去rs2中的值，并将结果写入rd";
                            break;
                            case "XOR":
                                des = "将rs1和rs2中的值按位进行异或操作，并将结果写入rd";
                             break;
                           case "ADDI":
                                des = "将rs1中的值和立即数相加，并将结果写入rd";
                             break;
                           case "ANDI":
                                des = "将rs1中的值和立即数按位进行与操作，并将结果写入rd";
                             break;
                           case "JALR":
                                des = "JALR(Jump And Link Register) 将rs1中的值加上立即数作为地址，将下一条命令的地址（PC+4）存放到rd，然后 将PC寄存器指向新地址";
                             break;
                           case "LB":
                                des = "LB(Load Byte) 将rs1中的值加上立即数作为地址，读取该地址中的字节值写入rd";
                             break;
                           case "LBU":
                                des = "LBU(Load Byte Unsinged) 将rs1中的值加上立即数作为地址，读取该地址中的字节值写入rd,无符号";
                             break;
                           case "LH":
                                des = "LH(Load HalfWord) 将rs1中的值加上立即数作为地址，从该地址开始读取16位的值写入rd";
                             break;
                           case "LHU":
                                des = "LHU(Load HalfWord Unsigned) 将rs1中的值加上立即数作为地址，从该地址开始读取16位的值写入rd，无符号";
                             break;
                           case "LW":
                                des = "LW(Load Word) 将rs1中的值加上立即数作为地址，从该地址开始读取32位的值写入rd";
                             break;
                           case "ORI":
                                des = "将rs1中的值和立即数按位进行或操作，并将结果写入rd";
                             break;
                           case "SLLI":
                                des = "SLLI(Shift Left Logical Immediate) 将rs1的值逻辑左移立即数中的值，并将结果写入rd<br/>立即数只取低五位，因为32位的数据最大也就移动2的5次方=32位，相当于R类型指令中rs2的位置<br/>立即数的高七位固定位0000000";
                             break;
                           case "SLTI":
                                des = "SLTI(Set Less Then Immediate)如果rs1的值小于立即数，rd的值设为1，否则rd的值设为0";
                             break;
                           case "SLTIU":
                                des = "SLTIU(Set Less Then Immediate Unsigned)如果rs1的值小于立即数(无符号)，rd的值设为1，否则rd的值设为0<br/>特殊处理，当Immediate是1，如果rs1是x0,则rd的值为1，否则rd的值为0";
                             break;
                           case "SRAI":
                                des = "SRAI(Shift Right Arithmetic Immediate) 将rs1的值算数右移立即数，并将结果写入rd，算数右移意味着如果是正数，左边补零，如果是负数，左边补1<br/>立即数只取低五位，因为32位的数据最大也就移动2的5次方=32位，相当于R类型指令中rs2的位置<br/>立即数的高七位固定位0100000";
                             break;
                           case "SRLI":
                                des = "SRLI(Shift Right Logical Immediate) 将rs1的值逻辑右移立即数中的值，并将结果写入rd，逻辑右移意味着不管正数还是负数，左边都补0<br/>立即数只取低五位，因为32位的数据最大也就移动2的5次方=32位，相当于R类型指令中rs2的位置<br/>立即数的高七位固定位0000000";
                             break;
                           case "XORI":
                                des = "将rs1中的值和立即数按位进行异或与操作，并将结果写入rd<br/>如果立即数是-1，表示非操作";
                             break;
                           case "BEQ":
                                des = "BEQ(Branch Equal) 如果rs1和rs2中的值相等，则跳转到新地址，新地址是当前PC加上立即数的两倍";
                             break;
                           case "BGE":
                                des = "BGE(Branch Great or Equal) 如果rs1的值大于等于rs2中的值，则跳转到新地址，新地址是当前PC加上立即数的两倍";
                             break;
                           case "BGEU":
                                des = "BGEU(Branch Great or Equal Unsigned) 如果rs1的值大于等于rs2中的值(无符号)，则跳转到新地址，新地址是当前PC加上立即数的两倍";
                             break;
                           case "BLT":
                                des = "BLT(Branch Less Than) 如果rs1的值小于rs2中的值，则跳转到新地址，新地址是当前PC加上立即数的两倍";
                             break;
                           case "BLTU":
                                des = "BLTU(Branch Less Than Unsigned) 如果rs1的值小于rs2中的值(无符号)，则跳转到新地址，新地址是当前PC加上立即数的两倍";
                             break;
                           case "BNE":
                                des = "BNE(Branch Not Equal) 如果rs1和rs2中的值不相等，则跳转到新地址，新地址是当前PC加上立即数";
                             break;
                           case "JAL":
                                des = "JAL(Jump And Link) 将当前PC中的值加上立即数的两倍作为地址，将下一条命令的地址（PC+4）存放到rd，然后 将PC寄存器指向新地址";
                             break;
                           case "SB":
                                des = "SB(Save Byte) 将rs1中的值加上立即数作为地址，把rs2中低8位的数据写入该地址";
                             break;
                           case "SH":
                                des = "SH(Save Halfword) 将rs1中的值加上立即数作为地址，把rs2中低16位的数据写入该地址";
                             break;
                           case "SW":
                                des = "SW(Save Word) 将rs1中的值加上立即数作为地址，把rs2中的数据写入该地址";
                             break;
                           case "AUIPC":
                                des = "AUIPC (Add Upper Immediate To PC)将立即数作为一个32位数的前20位，后12位补0，再加上当前PC的值，将结果写入rd";
                             break;
                           case "LUI":
                                des = "LUI (Load Upper Immediate)将立即数作为一个32位数的前20位，后12位补0，这样就形成了一个很大的数，将结果写入rd";
                            break;
                            default:
                            break;
                        }
                        var bookdes = document.getElementById('bookdes');
                        bookdes.innerHTML = des;
                    }
                </script>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button id="book" type="button" class="btn btn-primary">确定</button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="writewindow" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="staticBackdropLabel">指定地址</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="mb-3">
                    <label for="exampleFormControlTextarea1" class="form-label">起始地址</label>
                    <input class="form-control" id="startaddr" value="0x64" />
                </div>
                 <div class="mb-3">
                    <label for="exampleFormControlTextarea1" class="form-label">文本</label>
                    <input type="checkbox" class="form-check-input" id="istext" value="text" />
                </div>
                </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button id="write" type="button" class="btn btn-primary">确定</button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="debugwindow" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="staticBackdropLabel">指定地址</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="mb-3">
                    <label for="exampleFormControlTextarea1" class="form-label">断点地址，多地址用逗号分隔</label>
                    <input class="form-control" id="debugaddr" value="0x00" />
                </div>
                </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button id="debug" type="button" class="btn btn-primary">确定</button>
            </div>
        </div>
    </div>
</div>

<script src="~/js/signalr/dist/browser/signalr.js"></script>
<script src="~/js/info.js"></script>

